package com.bee.gateway.service.impl;

import com.alibaba.fastjson.JSON;
import com.bee.commons.cache.CacheService;
import com.bee.gateway.common.constants.GatewayRedisConst;
import com.bee.gateway.common.dto.MsgContentDTO;
import com.bee.gateway.common.enums.RoutesMsgTypeEnum;
import com.bee.gateway.mapper.RoutesMapper;
import com.bee.gateway.common.domain.RoutesDO;
import com.bee.gateway.common.dto.RouteDefinitionDTO;
import com.bee.gateway.common.enums.StatusEnum;
import com.bee.gateway.common.vo.RouteQueryVO;
import com.bee.gateway.service.RoutesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 路由配置
 * @author Damin.yang
 * @date 2019/07/27 23:27
 */
@Service
@Slf4j
public class RoutesServiceImpl implements RoutesService {

    @Autowired
    private RoutesMapper routesMapper;
    @Autowired
    private CacheService cacheService;

    @Override
    public int insert(RoutesDO routesDO) {
        routesDO.setStatus(StatusEnum.Y.name());
        int result = routesMapper.insert(routesDO);
        if (result == 1) {
            cacheService.set(GatewayRedisConst.ROUTES_PREFIX + routesDO.getId(),
                    JSON.toJSONString(new RouteDefinitionDTO(routesDO)));

            MsgContentDTO dto = new MsgContentDTO(RoutesMsgTypeEnum.SAVE.name(), String.valueOf(routesDO.getId()));
            cacheService.publish(GatewayRedisConst.ROUTES_CHANNEL, dto);
        }

        return result;
    }

    @Override
    public RoutesDO get(long id) {
        return routesMapper.get(id);
    }

    @Override
    public List<RoutesDO> query(RouteQueryVO queryVO) {
        return routesMapper.query(queryVO);
    }

    @Override
    public void update(RoutesDO routesDO) {
        routesMapper.update(routesDO);

        cacheService.set(GatewayRedisConst.ROUTES_PREFIX + routesDO.getId(),
                JSON.toJSONString(new RouteDefinitionDTO(routesDO)));

        MsgContentDTO dto = new MsgContentDTO(RoutesMsgTypeEnum.SAVE.name(), String.valueOf(routesDO.getId()));
        cacheService.publish(GatewayRedisConst.ROUTES_CHANNEL, dto);
    }

    @Override
    public void delete(long id) {
        routesMapper.delete(id);
        cacheService.del(GatewayRedisConst.ROUTES_PREFIX + id);
        MsgContentDTO dto = new MsgContentDTO(RoutesMsgTypeEnum.DELETE.name(), String.valueOf(id));
        cacheService.publish(GatewayRedisConst.ROUTES_CHANNEL, dto);
    }

    @Override
    public void overload() {
        // 重新load所有路由信息到缓存
        cacheAllRoutes();
        // 发消息通知路由服务器重载路由数据
        MsgContentDTO dto = new MsgContentDTO(RoutesMsgTypeEnum.OVERLOAD.name());
        cacheService.publish(GatewayRedisConst.ROUTES_CHANNEL, dto);
    }

    @Override
    public void cacheAllRoutes() {
        RouteQueryVO queryVO = new RouteQueryVO();
        List<RoutesDO> routes = query(queryVO);
        routes.forEach(route ->
                cacheService.set(GatewayRedisConst.ROUTES_PREFIX + route.getId(),
                        JSON.toJSONString(new RouteDefinitionDTO(route))));
    }
}
